import { WEBSOCKETS, toSuccess, useEntityManager } from "@sker/core";
import { useData } from "@sker/runtime";
import { InjectionToken, container } from "tsyringe";
import { z } from "zod";

export default async () => {
    const input = z.object({
        id: z.string(),
        code: z.string(),
        timeout: z.number()
    });
    const body = input.parse(useData());
    await useEntityManager(async m => {
        const websockets = container.resolve(WEBSOCKETS)
        websockets.forEach(wss => {
            wss.send(JSON.stringify({
                type: CLOUD_CODE,
                payload: {
                    id: body.id,
                    code: body?.code,
                    timeout: body.timeout
                }
            }))
        })
    })
    return toSuccess(true)
}
interface ICloudCode {
    id: string;
    code: string;
    timeout?: number;
}
const CLOUD_CODE: InjectionToken<ICloudCode> = `CLOUD_CODE`
